use "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 3 Background\Data\changeall.dta", replace

gen lntime=ln(time)
reg mex2006 lntime
predict mex2006hat if e(sample)==1
reg bra2014 lntime
predict bra2014hat if e(sample)==1
reg bra2002 lntime
predict bra2002hat if e(sample)==1
reg bra2010 lntime
predict bra2010hat if e(sample)==1
reg mex2000 lntime
predict mex2000hat if e(sample)==1

replace mex2006waves="06"
replace bra2002waves="02"
replace bra2014waves="14"
replace bra2010waves="10"
replace mex2000waves="00"

replace mex2006waves="{bf:06}" if time==39
replace mex2000waves="{bf:00}" if time==19
replace bra2002waves="{bf:02}" if time==27
replace bra2014waves="{bf:14}" if time==18
replace bra2010waves="{bf:10}" if time==27
replace mex2012waves="{bf:12}"
replace mex2018waves="{bf:18}"
replace bra2006waves="{bf:06}"
replace arg2015waves="{bf:15}"
replace bra2018waves="{bf:18}"


gen xarrow10s = 17
gen yarrow10s = .43
gen xarrow10e = 18
gen yarrow10e = .40
gen xarrow02s = 24
gen yarrow02s = .38
gen xarrow02e = 23.5
gen yarrow02e = .355
gen xarrow14s = 19
gen yarrow14s = .297
gen xarrow14e = 17.8
gen yarrow14e = .321
gen xarrow00s = 12
gen yarrow00s = .25
gen xarrow00e = 13
gen yarrow00e = .23
gen xarrow06s = 24
gen yarrow06s = .215
gen xarrow06e = 23.5
gen yarrow06e = .245
gen xarrow06bs = 11.3
gen yarrow06bs = .343
gen xarrow06be = 12
gen yarrow06be = .312
gen xarrow12s = 6.4
gen yarrow12s = .335
gen xarrow12e = 8.5
gen yarrow12e = .323

twoway (scatter bra2002 bra2002hat time, connect(i l) sort(time) lpattern(solid solid) lcolor(black black) lwidth(med med) m(i i) mc(black black)  mlab(bra2002waves) mlabpos(0 0) mlabc(black black)) /// 
(scatter bra2010 bra2010hat time, connect(i l) sort(time) lpattern(dash dash) lcolor(black black) lwidth(med med) m(i i) mc(black black)  mlab(bra2010waves) mlabpos(0 0) mlabc(black black)) ///
(scatter bra2014 bra2014hat time, connect(i l) sort(time) lpattern(shortdash shortdash) lcolor(black black) lwidth(med med) m(i i)  mc(black black)  mlab(bra2014waves) mlabpos(0 0) mlabc(black black)) ///
(scatter mex2000 mex2000hat time, connect(i l) sort(time) lcolor(gs8 gs8) lwidth(med med) m(i i) mc(gs8 gs8) mlab(mex2000waves) mlabpos(0 0) mlabc(gs8 gs8)) ///
(scatter mex2006 mex2006hat time, connect(i l) sort(time) lpattern(dash dash) lcolor(gs8 gs8) lwidth(med med) m(i i) mc(gs8 gs8) mlab(mex2006waves) mlabpos(0 0) mlabc(gs8 gs8)) ///
(scatter bra2006 time, m(i) mc(black)  mlab(bra2006waves) mlabc(black) mlabpos(0)) ///
(scatter mex2012 time, m(i) mc(gs8) mlab(mex2012waves) mlabc(gs8) mlabpos(0)) ///
(scatter mex2018 time, m(i) mc(gs8) mlab(mex2018waves) mlabc(gs8) mlabpos(0)) ///
(scatter arg2015 time, m(i) mc(gs8) mlab(arg2015waves) mlabc(black) mlabpos(0)) ///
(scatter bra2018 time, m(i) mc(black) mlab(bra2018waves) mlabc(black) mlabpos(0)) ///
, /// 
xtitle("{bf:Weeks between Waves}") ytitle("{bf:Percentage of Twice-Opinionated}" "{bf:Respondents who Change Vote Preference}") yscale(range(0 .4)) graphregion(color(white)) ///
plotregion(lstyle(yxline) lcolor(black)) legend(order(6 "Brazil" 2 "Mexico" ) size(med) rows(2) ///
symxsize(8) keygap(2) colgap(1)) ytick(0(.1).4) ylabel(0 "0%" .1 "10%" .2 "20%" .3 "30%" .4 "40%" .5 "50%") ///
legend(order (2 "Brazil 2002" 4 "Brazil 2010" 6 "Brazil 2014" 8 "Mexico 2000" 10 "Mexico 2006") size(medsmall) title("Best-fit Lines", size(medsmall) color(black)))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 3 Background\Figures\ChangeFigure.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR3_1.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR3_1.pdf", as(pdf) replace 
xyz


egen bra=rmax( bra2002 bra2006 bra2010 bra2014)
egen mex=rmax( mex2000 mex2006 mex2012)

/*
text(.34 3.5 "Mexico 2012", size(small) lcolor(white) color(gs8)) ///
text(.35 9 "Brazil 2006", size(small) lcolor(white) color(black)) 
(pcarrow yarrow06bs xarrow06bs yarrow06be xarrow06be, lcolor(black) mcolor(black)) ///
(pcarrow yarrow12s xarrow12s yarrow12e xarrow12e, lcolor(gs8) mcolor(gs8)) ///


(pcarrow yarrow10s xarrow10s yarrow10e xarrow10e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow02s xarrow02s yarrow02e xarrow02e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow14s xarrow14s yarrow14e xarrow14e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow00s xarrow00s yarrow00e xarrow00e, lcolor(gs8) mcolor(gs8)) ///
(pcarrow yarrow06s xarrow06s yarrow06e xarrow06e, lcolor(gs8) mcolor(gs8)) ///
text(.29 19 "Bra14", size(small) lcolor(white))  text(.44 17 "Bra10", size(small) lcolor(white)) ///
text(.39 24 "Bra02", size(small) lcolor(white)) ///
text(.21 24 "Mex06", size(small) lcolor(white) color(gs8)) ///
text(.26 12 "Mex00", size(small) lcolor(white) color(gs8))  

